#include "OrbitManager.h"


static OrbitManager*	s_sharedOrbitManager = NULL;


OrbitManager* OrbitManager::sharedOrbitManager( )
{
	if( s_sharedOrbitManager == NULL ){
		s_sharedOrbitManager = new OrbitManager( );

		s_sharedOrbitManager->setContentSize( CCDirector::sharedDirector()->getVisibleSize() );
		NotificationCenter::sharedNotificationCenter()->registerHandlerMessage(s_sharedOrbitManager);
	}

	return s_sharedOrbitManager;
}


OrbitManager::OrbitManager( ) : m_pListOrbit(0) , m_fOrbitRadius(0) , m_iNumShow(0)
{

}

OrbitManager::~OrbitManager( )
{
	if( m_pListOrbit ){
		m_pListOrbit->removeAllObjects( );
		m_pListOrbit->release( );
	}

	if( m_fOrbitRadius )
	{
		CC_SAFE_DELETE(m_fOrbitRadius);
	}

	NotificationCenter::sharedNotificationCenter()->unregisterHandlerMessage(this);
}



void OrbitManager::initWithNumOrbitSystem( int num , int numShow , const std::vector<float>* listRadius )
{
	if( m_pListOrbit )
	{
		m_pListOrbit->removeAllObjects( );
		m_pListOrbit->initWithCapacity(num);
	}else{
		m_pListOrbit = new CCArray(num);
	}

	m_iNumShow = numShow;

	m_fOrbitRadius = new float[numShow];

	for( int i = 0 ; i < numShow ; ++i ){
		m_fOrbitRadius[i] = listRadius->at(i);
	}
}


void OrbitManager::addOrbitSystem( OrbitSystem* pOrbit )
{
	if( m_pListOrbit ){
		m_pListOrbit->addObject( pOrbit );

		int count = m_pListOrbit->count();

		if( count <= m_iNumShow && count > 0  )
		{
			pOrbit->setRadius( m_fOrbitRadius[count - 1]);
			pOrbit->setTag( ORBIT_ACTIVE );
		}else{
			pOrbit->setVisible(false);
			pOrbit->setTag( ORBIT_UN_ACTIVE );
		}

		pOrbit->setPosition( ccp( this->getContentSize().width / 2 , this->getContentSize().height / 2 ) );

		if( !pOrbit->getParent() ){
			this->addChild( pOrbit );
		}
	}
}


void OrbitManager::removeOrbitSystem( OrbitSystem* pOrbit )
{
	if( m_pListOrbit ){
		int startIdx = m_pListOrbit->indexOfObject(pOrbit);
		m_pListOrbit->removeObject(pOrbit);

		//pOrbit->setVisible(false);
		pOrbit->setTag( ORBIT_UN_ACTIVE );

		//Run move animation from startIdx to startIdx + num Orbit - 1
		runShowOrbitAnimation( startIdx );
	}
}


void OrbitManager::runShowOrbitAnimation( int startIdx  )
{
	int idx = 0;
	CCObject* pObj = NULL;
	CCARRAY_FOREACH( m_pListOrbit , pObj )
	{
		if( idx >= startIdx ){
			OrbitSystem* pOrbit = (OrbitSystem*)pObj;
			if( pOrbit->getTag() == ORBIT_UN_ACTIVE )
			{
				pOrbit->setTag( ORBIT_ACTIVE );
				pOrbit->setVisible(true);
				pOrbit->setRadius( m_fOrbitRadius[m_iNumShow-1] + ( m_fOrbitRadius[m_iNumShow-1] - m_fOrbitRadius[m_iNumShow-2] ) );
			}
			pOrbit->runToOriginAnimation( m_fOrbitRadius[idx] );
		}
		idx += 1;
		if( idx >= m_iNumShow ){
			break;
		}
	}
}



OrbitSystem*			OrbitManager::getActiveOrbit			( )
{
	return dynamic_cast<OrbitSystem*>( this->getChildByTag(ORBIT_ACTIVE) );
}


OrbitSystem*			OrbitManager::getUnActiveOrbit		( )
{
	return dynamic_cast<OrbitSystem*>( this->getChildByTag(ORBIT_UN_ACTIVE) ); 
}


void OrbitManager::onHandlerMessage	( NotificationData* data , int messageID  , int messageSubId )
{
	switch(messageID){
	case NOTI_ON_REMOVE_ORBIT:
		{
			OrbitSystem* pOrbit = dynamic_cast<OrbitSystem*>(data->getObject(NOTIOBJECT_NAME_ORBIT));
			if( pOrbit ){
				this->removeOrbitSystem( pOrbit );
			}
		}break;
	}
}